#ifndef _SEREVER_H
#define _SEREVER_H

#include "CertificateManager.h"
#include "Channel.h"
#include "ECCOper.h"
#include "EventLoop.h"
#include "EventLoopThreadPool.h"
#include "HandshakeTaskQueue.h"
#include "HandshakeThreadPool.h"
#include "MessageDispatcher.h"
#include "blockchainoperator.h"
#include "thread.h"
#include "utils.h"

#include <any>
#include <condition_variable>
#include <memory>
#include <openssl/ec.h>
#include <openssl/types.h>
#define QUEUE_NAME "/My_mesg_p"
namespace droneswarm
{

    class Server
    {
    public:
        Server( EventLoop *loop, int threadNum, int port );
        ~Server();
        EventLoop *getLoop() const { return loop_; }
        void start();
        void handleNewConn();
        void handleThisConn() { loop_->updatePoller( accpetChannel_ ); }
        void processShakeHandTask( std::any arg );
        //定义一个握手manager线程来执行握手的相关逻辑
        void shakeHandManagerThread();

    private:
        bool startPOSIXMsgQueue();

    private:
        EventLoop *loop_;
        MessageDispatcher *dispatcher_;
        int threadNum_;
        std::unique_ptr< EventLoopThreadPool > eventLoopThreadPool_;
        bool started_;
        std::shared_ptr< Channel > accpetChannel_;
        int port_;
        int listenFd_;
        static const int MAXFDS = 100000;
        //生成服务端的公私钥在server类写好，如果写进了httpData的话，每次连接都加载就浪费资源了
        ECCOperator ecc_;
        CertificateManager cert_;
        EVP_PKEY *EccKeyPair_;
        EVP_PKEY *ServerPrivKey_;
        X509 *ServerCert_;
        BIGNUM *serverPriBn_;
        // HandshakeTaskQueue TaskQueue_;
        std::unique_ptr< HandshakeTaskQueue > TaskQueue_;
        Thread shakeHandManager_;
        mqd_t mq_;              //这是消息队列的标识符
        BlockOpMsg blockData_;  //消息队列中消息的格式。
        // std::condition_variable cv_;
    };
}  // namespace droneswarm

#endif